package com.esjob.config;

import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.dataflow.DataflowJobConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.api.JobScheduler;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter;
import com.esjob.task.MyDataFlowTask2;
import com.esjob.task.MyTask1;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/19  8:55
 * 描述:
 */
@Configuration
public class EsConfig3 {

    @Autowired
    private EsConfigEntity esConfigEntity;

    @Autowired
    private MyDataFlowTask2 myTask1;

    /**
     * 作者: LCG
     * 日期: 2021/3/19  8:56
     * 描述: 配置Zookeeper的配置
     * @Param []
     * @Return com.dangdang.ddframe.job.reg.zookeeper.ZookeeperConfiguration
     */
    @Bean
    public ZookeeperConfiguration getZookeeperConfiguration(){

        ZookeeperConfiguration zookeeperConfiguration=
                new ZookeeperConfiguration(esConfigEntity.getZookeeperList(),esConfigEntity.getNamesapace());

        return zookeeperConfiguration;
    }


    /**
     * 作者: LCG
     * 日期: 2021/3/19  8:59
     * 描述: 配置和初始化Zookeeper注册中心
     * @Param [zookeeperConfiguration]
     * @Return com.dangdang.ddframe.job.reg.zookeeper.ZookeeperRegistryCenter
     */
    @Bean(initMethod = "init")
    public ZookeeperRegistryCenter getZookeeperRegistryCenter(ZookeeperConfiguration zookeeperConfiguration){

        ZookeeperRegistryCenter zookeeperRegistryCenter=new ZookeeperRegistryCenter(zookeeperConfiguration);

        return zookeeperRegistryCenter;
    }


    /**
     * 作者: LCG
     * 日期: 2021/3/19  9:00
     * 描述: 作业核心配置
     * @Param []
     * @Return com.dangdang.ddframe.job.config.JobCoreConfiguration
     */
    @Bean
    public JobCoreConfiguration getJobCoreConfiguration(){
        JobCoreConfiguration build = JobCoreConfiguration.newBuilder(esConfigEntity.getJobName(), esConfigEntity.getCron(), esConfigEntity.getFenpian())
                .jobParameter("job-parameter")
                .shardingItemParameters("0=T0,1=T1")
                .failover(true)
                .description("任务的描述")
                .build();

        return build;
    }

    /**
     * 作者: LCG
     * 日期: 2021/3/19  9:09
     * 描述: 配置作业
     * @Param [jobCoreConfiguration]
     * @Return com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration
     */
    @Bean
    public DataflowJobConfiguration getSimpleJobConfiguration(JobCoreConfiguration jobCoreConfiguration){
        DataflowJobConfiguration simpleJobConfiguration=new DataflowJobConfiguration(jobCoreConfiguration,MyDataFlowTask2.class.getCanonicalName(),true);
        return simpleJobConfiguration;
    }

    /**
     * 作者: LCG
     * 日期: 2021/3/19  9:10
     * 描述: 配置轻量作业
     * @Param []
     * @Return com.dangdang.ddframe.job.lite.config.LiteJobConfiguration
     */
    @Bean
    public LiteJobConfiguration getLiteJobConfiguration(DataflowJobConfiguration dataflowJobConfiguration){
        LiteJobConfiguration build = LiteJobConfiguration.newBuilder(dataflowJobConfiguration).build();
        return build;
    }


    /**
     * 作者: LCG
     * 日期: 2021/3/19  9:14
     * 描述: 配置作业调度器
     * @Param []
     * @Return com.dangdang.ddframe.job.lite.api.JobScheduler
     */
    @Bean(initMethod ="init" )
    public JobScheduler getJobScheduler(LiteJobConfiguration liteJobConfiguration,ZookeeperRegistryCenter zookeeperRegistryCenter){

        SpringJobScheduler springJobScheduler=new SpringJobScheduler(myTask1,zookeeperRegistryCenter,liteJobConfiguration);

        return springJobScheduler;

    }

}
